package com.ruoyi.system.xcenter.endpoints;

import com.ruoyi.common.json.JSON;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.system.domain.DataxXchangeLog;
import com.ruoyi.system.mapper.DataxXchangeLogMapper;
import com.ruoyi.system.xcenter.EPContext;
import com.ruoyi.system.xcenter.IEndPoint;
import com.ruoyi.system.xcenter.PassiveEndPointAbstract;
import com.ruoyi.system.xcenter.exception.DataXException;
import com.ruoyi.system.xcenter.utils.ParkcodeMapConstants;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class JieshunEndPoint extends PassiveEndPointAbstract implements IEndPoint {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DataxXchangeLogMapper dataxXchangeLogMapper;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @Override
    public Object dataUpload(EPContext context, Object body, Integer type) {
        // TODO 捷顺数据上行
        return null;
    }

    @PostMapping(value = {"/jieshun/{module}/{function}","/jieshun/{module}/{submodule}/{function}"})
    @ResponseBody
    @Override
    public Object dataDownload( EPContext context,Object request, Integer type)  {
        String uri = httpServletRequest.getRequestURI();
        JSONObject result = new JSONObject();
        DataxXchangeLog xchangeLog = new DataxXchangeLog();
        xchangeLog.setEndGroupId(1l);
        try {
            String body = IOUtils.toString(httpServletRequest.getReader());
            logger.info("jieshun api post {} push body : {}",uri,body);

            switch (uri){
                case "/jieshun/park/parkInfo":
                case "/jieshun/park/parkSpace":
                    // 车位数对接、心跳转换
                    this.consumeEndPoint(body, 1);
                    break;
                case "/jieshun/park/parkIn":
                    // 入场对接
                    this.consumeEndPoint(body, 2);
                    break;
                case "/jieshun/park/parkOut":
                    // 入场对接
                    this.consumeEndPoint(body, 3);
                    break;
                case "/jieshun/park/parkOrder":
                    // 订单接入-限制-商户号的订单接入
                    this.consumeEndPoint(body, 4);
                    break;
                case "/jieshun/parking/querysign":
                    // TODO 验签代扣接口
                    querySign(result,body);
                    break;
                case "/jieshun/person/personInfo":
                case "/jieshun/park/parkCharge":
                case "/jieshun/park/lockOut":
                case "/jieshun/equip/baseInfo":
                case "/jieshun/place/status":
                case "/jieshun/person/reCar":
                    logger.info("{},暂未接入，{}",uri,body);
                    xchangeLog.setErrorLog(uri+" 暂未接入");
                    xchangeLog.setClientEndReq(body);
                    xchangeLog.setXchangeStart(new Date());

                    throw new DataXException(uri+"-暂未接入:"+body);
            }
        }catch (Exception e){
            if(e instanceof DataXException){
                //
            }else {
                xchangeLog.setErrorLog(uri+" 解析异常:"+ ExceptionUtil.getExceptionMessage(e));
                logger.info("jieshun api post error : {}",e);
            }
            dataxXchangeLogMapper.insertDataxXchangeLog(xchangeLog);
        }

        result.set("resultCode",0);
        result.set("message","处理成功");

        return result;
    }

    private void querySign(JSONObject result,String body) throws Exception {
        JSONObject unmarshal = JSON.unmarshal(body, JSONObject.class);
        String businessNo = unmarshal.getObj("data").getStr("businessNo");
        String carNumber = unmarshal.getObj("data").getStr("carNumber");
        String parkCode = unmarshal.getObj("data").getStr("parkCode");
        String parkName = unmarshal.getObj("data").getStr("parkName");
        String inTime = unmarshal.getObj("data").getStr("inTime");
        if(ParkcodeMapConstants.BUSINESS_NO.equals(businessNo)){
            // 无感验签
            logger.info("无感支付车牌号：{}",carNumber);
            // 信电调用
            EPContext context = (EPContext) consumer.get(CONTEXT);
            XindianEndPoint endPoint = (XindianEndPoint) consumer.get(END_POINT);
            JSONObject req = new JSONObject();
//            Object res = endPoint.dataUpload(context, req, 7);
            result.put("status",0);
        }else {
            // 不支持的商户号
            result.put("status",1);
        }

    }


}
